/**
 * @file   Common.h
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2013-03-10
 * @brief  DataBus Common.
 *
 * Copyright (C) 2013  Djuro Drljaca <djurodrljaca@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef LIBDATABUS_COMMON_COMMON_H
#define LIBDATABUS_COMMON_COMMON_H

#include <QtCore/QByteArray>

namespace DataBus
{
#if defined(DATABUS_LIBRARY)
#  define DATABUSSHARED_EXPORT Q_DECL_EXPORT
#else
#  define DATABUSSHARED_EXPORT Q_DECL_IMPORT
#endif

#define RETURN_STATUS_SUCCESS               ((quint8)0x00u)
#define RETURN_STATUS_ERROR                 ((quint8)0x01u)
#define RETURN_STATUS_INVALID_CLIENT_ID     ((quint8)0x02u)
#define RETURN_STATUS_INVALID_PROPERTY_ID   ((quint8)0x02u)
#define RETURN_STATUS_INVALID_METHOD_ID     ((quint8)0x02u)

#define STRING_MAX_LENGTH       (254)
#define BYTE_ARRAY_MAX_LENGTH   (254)

/**
 * @brief The PacketType enum
 */
enum PacketType
{
    PacketType_Error = 0,   /**< Error Packet */

    PacketType_PingRequest,     /**< Ping Request Packet */
    PacketType_PingResponse,    /**< Ping Response Packet */

    PacketType_RegisterClientRequest,   /**< RegisterClient Request Packet */
    PacketType_RegisterClientResponse,  /**< RegisterClient Response Packet */

    PacketType_GetClientListRequest,    /**< GetClientList Request Packet */
    PacketType_GetClientListResponse,   /**< GetClientList Response Packet */

    PacketType_GetClientNameRequest,    /**< GetClientName Request Packet */
    PacketType_GetClientNameResponse,   /**< GetClientName Response Packet */

    PacketType_GetClientStatusRequest,  /**< GetClientStatus Request Packet */
    PacketType_GetClientStatusResponse, /**< GetClientStatus Response Packet */

    PacketType_GetClientPropertyListRequest,  /**< GetClientPropertyList Request Packet */
    PacketType_GetClientPropertyListResponse, /**< GetClientPropertyList Response Packet */

    PacketType_GetClientPropertyInfoRequest,  /**< GetClientPropertyInfo Request Packet */
    PacketType_GetClientPropertyInfoResponse, /**< GetClientPropertyInfo Response Packet */

    PacketType_GetClientPropertyValueRequest,    /**< GetClientPropertyValue Request Packet */
    PacketType_GetClientPropertyValueResponse,   /**< GetClientPropertyValue Response Packet */

    PacketType_SetClientPropertyValueRequest,    /**< SetClientPropertyValue Request Packet */
    PacketType_SetClientPropertyValueResponse,   /**< SetClientPropertyValue Response Packet */

    PacketType_GetClientMethodListRequest,  /**< GetClientMethodList Request Packet */
    PacketType_GetClientMethodListResponse, /**< GetClientMethodList Response Packet */

    PacketType_GetClientMethodInfoRequest,  /**< GetClientMethodInfo Request Packet */
    PacketType_GetClientMethodInfoResponse, /**< GetClientMethodInfo Response Packet */

    PacketType_GetClientMethodParameterInfoRequest,     /**< GetClientMethodParameterInfo Request Packet */
    PacketType_GetClientMethodParameterInfoResponse,    /**< GetClientMethodParameterInfo Response Packet */

    PacketType_GetClientMethodReturnValueInfoRequest,   /**< GetClientMethodReturnValueInfo Request Packet */
    PacketType_GetClientMethodReturnValueInfoResponse,  /**< GetClientMethodReturnValueInfo Response Packet */

    PacketType_ExecuteClientMethodRequest,  /**< ExecuteClientMethod Request Packet */
    PacketType_ExecuteClientMethodResponse  /**< ExecuteClientMethod Response Packet */
};


/**
 * @brief The DataType enum
 */
enum DataType
{
    DataType_Invalid = -1,  /**< Invalid data type */

    DataType_Boolean = 0,   /**< Boolean data type */

    DataType_Int8,   /**< Signed 8-bit integer data type */
    DataType_UInt8,   /**< Unsigned 8-bit integer data type */

    DataType_Int16,   /**< Signed 16-bit integer data type */
    DataType_UInt16,   /**< Unsigned 16-bit integer data type */

    DataType_Int32,   /**< Signed 32-bit integer data type */
    DataType_UInt32,   /**< Unsigned 32-bit integer data type */

    DataType_Int64,   /**< Signed 64-bit integer data type */
    DataType_UInt64,   /**< Unsigned 64-bit integer data type */

    DataType_Float,   /**< Floating-point value (32-bit) data type */
    DataType_Double,   /**< Double floating-point value (64-bit) data type */

    DataType_String,   /**< String (UTF-8 encoding) data type */

    DataType_ByteArray   /**< Byte Array data type */
};


/**
 * @brief The ClientStatus enum
 */
enum ClientStatus
{
    ClientStatus_Active = 0,        /**< Client is active */
    ClientStatus_Inactive,          /**< Client is inactive */
    ClientStatus_ErrorUnknownClient /**< Error, unknown client */
};


/**
 * @brief The AccessPermission enum
 */
enum AccessPermission
{
    AccessPermission_Invalid = -1,  /**< Invalid permission */
    AccessPermission_ReadOnly = 0,  /**< Read-only permission */
    AccessPermission_WriteOnly,     /**< Write-only permission */
    AccessPermission_ReadWrite      /**< Read/Write permission */
};

/**
 * @brief Calculate checksum from input data
 *
 * @param data          Input data
 * @param startIndex    Start index if input data
 * @param size          Number of bytes used in the calculation (-1 for all)
 *
 * @param[out] ok   Status (true - success, false - error)
 *
 * @return Calculated checksum
 */
DATABUSSHARED_EXPORT quint8 calculateChecksum(const QByteArray &data,
                                              const int startIndex = 0,
                                              const int size = -1,
                                              bool *ok = 0);

}

#endif // LIBDATABUS_COMMON_COMMON_H
